#include <stdio.h>
#include "Sort.h"

void Sort::Swap(int& a, int& b)
{
	int temp = a;
	a = b;
	b = temp;
}

void Sort::Swap(char& a, char& b)
{
	char ch = a;
	a = b;
	b = ch;
}

int Sort::Compare(int a, int b)
{
	if (a < b) return -1;
	else if (a == b) return 0;
	else return 1;	
}

int Sort::SeqSearch(int list[], int searchnum, int n)
{
	for(int i=0; i<n; ++i) {
		if (list[i] == searchnum) return i;	
	}			

	return -1;
}

int Sort::IterativeBinSearch(int list[], int searchnum, int n)
{
	int left = 0;
	int right = n - 1;
	int middle;
	while(left <= right) {
		middle = (left+right)/2;
		switch(Compare(list[middle], searchnum)) {
			case -1: left = middle + 1;
			case 0: return middle;
			case 1: right = middle - 1;
		}						
	}	

	return -1;
} 

int Sort::RecursiveBinSearch(int list[], int searchnum, int left, int right)
{
	if (left > right) return -1;

	int middle = (left+right)/2;
	switch(Compare(list[middle], searchnum)) {
		case -1: return RecursiveBinSearch(list, searchnum, middle+1, right);
		case 0: return middle;
		case 1: return RecursiveBinSearch(list, searchnum, left, middle-1);
	}	
}

void Sort::Print(int list[], int n)
{
	for(int i=0; i<n; ++i) {
		printf("%d ", list[i]);				
	}	
	printf("\n");
}
